using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows;
using CoastToCoast.Game;
using CoastToCoast.Logic;

namespace CoastToCoast.Client {
    public class BoardView {
        private Board _board;
        public Board Board {
            get { return _board; }
            set { _board = value; }
        }

        private Canvas _canvas;
        public Canvas Canvas {
            get { return _canvas; }
            set { _canvas = value; }
        }

        private CellView[][] _views;
        public CellView[][] Views {
            get { return _views; }
            set { _views = value; }
        }

        private const int CellWidth = 40;
        private const int CellHeight = 40;
        private const int ImageLeftOffset = 2;
        private const int ImageHeightOffset = 2;
        public BoardView(Board board, Canvas BoardCanvas) {
            Board = board;
            Canvas = BoardCanvas;
            Canvas.MouseLeftButtonUp += new MouseEventHandler(rootCanvas_MouseLeftButtonUp);
        }

        public void RenderBoard() {
            Canvas.Children.Clear();
            Views = new CellView[Board.Rows][];
            int topOffset = 0;
            for (int row = 0; row < Board.Rows; row++) {
                _views[row] = new CellView[Board.Columns];
                int leftOffset = 0;
                for (int col = 0; col < Board.Columns; col++) {
                    Rectangle rect = new Rectangle();
                    rect.Width = CellWidth;
                    rect.Height = CellHeight;
                    rect.SetValue(Canvas.TopProperty, topOffset + ImageHeightOffset);
                    rect.SetValue(Canvas.LeftProperty, leftOffset + ImageLeftOffset);

                    Canvas.Children.Add(rect);
                    Canvas.Background = new SolidColorBrush(Colors.Red);
                    _views[row][col] = new CellView(Board.Cells[row][col], rect);
                    leftOffset += CellWidth;
                }
                topOffset += CellHeight;
            }
        }

        bool _clickOne = false;
        Cell _cellOne;
        Cell _cellTwo;

        private void rootCanvas_MouseLeftButtonUp(object sender, MouseEventArgs e) {
            Point pos = e.GetPosition(Canvas);
            int x = (int)(pos.X / 40);
            int y = (int)(pos.Y / 40);
            TextBlock playerName = Canvas.FindName("PlayerName") as TextBlock;
            if (!_clickOne) {
                playerName.Text = "Click #1 X:" + x + " Y: " + y;
                _cellOne = Board.Cells[y][x];
            }
            else {
                playerName.Text = "Click #2 X:" + x + " Y: " + y;
                _cellTwo = Board.Cells[y][x];


                if (_cellOne.Unit != null) {
                    Player player = _cellOne.Unit.Owner;
                
                    // Combat baby
                    if (_cellTwo.Unit != null) {
                        playerName.Text = "Combat!  X:" + x + " Y: " + y;
                        Combat.StartCombat(_cellOne.Unit, _cellTwo.Unit);
                        player.DoTurn();
                    }
                    else {
                        _cellOne.Unit.Move(_cellTwo);

                        // Conquer the city
                        if (_cellTwo.Terrain is City) {
                            player.AddCity((City)_cellTwo.Terrain);
                        }

                        player.DoTurn();
                    }

                    RenderBoard();
                }
            }
            _clickOne = !_clickOne;
        }

    }
}
